package com.ss.android.common.ui.view;

import android.content.Context;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;

import java.util.ArrayList;

import inline.ScreenshotListener;

public final class FragmentTabHost extends TabHost implements TabHost.OnTabChangeListener {
    public FragmentTabHost(Context context) {
        super(context);
    }

    static class SavedState extends View.BaseSavedState {
        public static final Parcelable.Creator CREATOR = new Creator<SavedState>() {
            public final SavedState createFromParcel(Parcel source) {
                return new SavedState(source);
            }

            public final SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };
        String curTab;

        SavedState(Parcelable arg1) {
            super(arg1);
        }

        private SavedState(Parcel parcel) {
            super(parcel);
            this.curTab = parcel.readString();
        }

        public String toString() {
            return "FragmentTabHost.SavedState{" + Integer.toHexString(System.identityHashCode(this)) + " curTab=" + this.curTab + "}";
        }

        public void writeToParcel(Parcel arg2, int arg3) {
            super.writeToParcel(arg2, arg3);
            arg2.writeString(this.curTab);
        }
    }

    public static final class FragmentTabHost$a {
        final String a;
        final Class b;
        final Bundle c;
        Fragment d;


        public FragmentTabHost$a(Fragment d, String a, Class b, Bundle c) {
            this.a = a;
            this.b = b;
            this.c = c;
            this.d = d;
        }
    }

    private boolean a;
    private final ArrayList b = null;
    private FrameLayout c;
    private TabHost.OnTabChangeListener d;
    private FragmentTabHost$a e;
    private boolean f;
    private boolean g;
    private boolean h;

    private FragmentTransaction a(String arg7, FragmentTransaction arg8) {
        FragmentManager v4 = null;
        this.h = true;
        int v2 = 0;
        Object v1;
        Object v0;
        for (v1 = v4; v2 < this.b.size(); v1 = v0) {
            v0 = this.b.get(v2);
            if (!((FragmentTabHost$a) v0).a.equals(arg7)) {
                v0 = v1;
            }
            ++v2;
        }

        if (v1 == null) {
            throw new IllegalStateException("No tab known for tag " + arg7);
        }

        if (this.e != v1) {
            if (arg8 == null) {
                arg8 = v4.beginTransaction();
            }

            if (this.e != null && this.e.d != null) {
                if (this.g) {
                    arg8.hide(this.e.d);
                } else {
                    arg8.detach(this.e.d);
                }
            }

            if (v1 != null) {
                if (((FragmentTabHost$a) v1).d == null) {
                    ((FragmentTabHost$a) v1).d = Fragment.instantiate(((Context) null), ((FragmentTabHost$a) v1).b.getName(), ((FragmentTabHost$a) v1).c);
                    arg8.add(0, ((FragmentTabHost$a) v1).d, ((FragmentTabHost$a) v1).a);
                } else if (this.g) {
                    if (((FragmentTabHost$a) v1).d.isDetached()) {
                        arg8.attach(((FragmentTabHost$a) v1).d);
                    }
                    if (!((FragmentTabHost$a) v1).d.isHidden()) {
                    } else {
                        arg8.show(((FragmentTabHost$a) v1).d);
                    }
                } else {
                    arg8.attach(((FragmentTabHost$a) v1).d);
                }
            }
            this.e = ((FragmentTabHost$a) v1);
        }

        return arg8;
    }

    public final void dispatchWindowFocusChanged(boolean arg4) {
        if (this.a) {
            int v1 = this.getChildCount();
            int v0;
            for (v0 = 0; v0 < v1; ++v0) {
                this.getChildAt(v0).dispatchWindowFocusChanged(arg4);
            }
        } else {
            super.dispatchWindowFocusChanged(arg4);
        }
    }

    public final Fragment getCurrentFragment() {
        Fragment v0 = this.e != null ? this.e.d : null;
        return v0;
    }

    public final boolean getDispatchWindowFocusChangedToAllTab() {
        return this.a;
    }

    protected final void onAttachedToWindow() {
        FragmentManager v3 = null;
        super.onAttachedToWindow();
        String v4 = this.getCurrentTabTag();
        int v2 = 0;
        FragmentTransaction v1 = ((FragmentTransaction) null);
        while (v2 < this.b.size()) {
            Object v0 = this.b.get(v2);
            ((FragmentTabHost$a) v0).d = v3.findFragmentByTag(((FragmentTabHost$a) v0).a);
            if (((FragmentTabHost$a) v0).d != null && !((FragmentTabHost$a) v0).d.isDetached()) {
                if (((FragmentTabHost$a) v0).a.equals(v4)) {
                    this.e = ((FragmentTabHost$a) v0);
                } else {
                    if (v1 == null) {
                        v1 = v3.beginTransaction();
                    }

                    v1.detach(((FragmentTabHost$a) v0).d);
                }
            }

            ++v2;
        }

        this.f = true;
        FragmentTransaction v0_1 = this.a(v4, v1);
        if (v0_1 != null) {
            v0_1.commitAllowingStateLoss();
            v3.executePendingTransactions();
        }
    }

    protected final void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        this.f = false;
    }

    public final void onFinishInflate() {
        int v3 = 16908307;
        int v6 = -1;
        super.onFinishInflate();
        Context v0 = this.getContext();
        if (v0 != null && this.findViewById(v3) == null) {
            LinearLayout v1 = new LinearLayout(v0);
            v1.setOrientation(1);
            this.addView(((View) v1), new FrameLayout.LayoutParams(v6, v6));
            TabWidget v2 = new TabWidget(v0);
            v2.setId(v3);
            v2.setOrientation(0);
            v1.addView(((View) v2), new LinearLayout.LayoutParams(v6, -2, 0f));
            FrameLayout v2_1 = new FrameLayout(v0);
            v2_1.setId(16908305);
            v1.addView(((View) v2_1), new LinearLayout.LayoutParams(0, 0, 0f));
            v2_1 = new FrameLayout(v0);
            this.c = v2_1;
            this.c.setId(0);
            v1.addView(((View) v2_1), new LinearLayout.LayoutParams(v6, 0, 1f));
        }
    }

    protected final void onRestoreInstanceState(Parcelable arg2) {
        super.onRestoreInstanceState(((SavedState) arg2).getSuperState());
        this.setCurrentTabByTag(((SavedState) arg2).curTab);
    }

    protected final Parcelable onSaveInstanceState() {
        SavedState v1 = new SavedState(super.onSaveInstanceState());
        v1.curTab = this.getCurrentTabTag();
        return ((Parcelable) v1);
    }

    public final void onTabChanged(String arg2) {
        if (this.f) {
            FragmentTransaction v0 = this.a(arg2, null);
            if (v0 != null) {
                v0.commitAllowingStateLoss();
            }
        }

        if (this.d != null) {
            this.d.onTabChanged(arg2);
        }
    }

    public final void setDispatchWindowFocusChangedToAllTab(boolean arg1) {
        this.a = arg1;
    }

    public final void setHideWhenTabChanged(boolean arg3) {
        if (!this.h) {
            this.g = arg3;
        } else {
            Log.w("FragmentTabHost", "setHideWhenTabChanged after onTabChanged, ignore");
        }
    }

    public final void setOnTabChangedListener(TabHost.OnTabChangeListener arg1) {
        this.d = arg1;
    }

    public final void setOnTabSwitchListener$2c8976b7(ScreenshotListener arg1) {
    }

    @Deprecated
    public final void setup() {
        throw new IllegalStateException("Must call setup() that takes a Context and FragmentManager");
    }
}

